﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UploadPlugin.Models;

namespace UploadPlugin.Controllers
{
 
    public class DownloadFileAttribute : ActionFilterAttribute
    {
        private const string DOWNLOAD_FILE_COOKIE = "downloadFile";
        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
 	          if(filterContext.Result is FileResult)
 	          {
                if (HttpContext.Current.Response.Cookies[DOWNLOAD_FILE_COOKIE] != null)
                {
                    HttpContext.Current.Response.Cookies[DOWNLOAD_FILE_COOKIE].Expires = DateTime.Now.AddYears(-1);
                }
                else
 	              {
                    HttpContext.Current.Response.Cookies.Add(new HttpCookie(DOWNLOAD_FILE_COOKIE, "true"));
 	              }
 	          }
            base.OnResultExecuted(filterContext);
        }
    }

    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            

            return View();
        }

        public ActionResult Download()
        {
            return View();
        }

        [DownloadFile]
        public ActionResult GetFile()
        {
            try
            {
                Report report = new Report();

                
                Response.Headers.Remove("Content-Disposition");
                Response.Headers.Add("Content-Disposition", "inline; filename=SimpleReportAgain");

                return File(report.GetReport(), //.GetAsByteArray(),
                            "application/pdf");
                //"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); //,
                //report.Name + ".xlsx");
            }
            catch (Exception exception)
            {
                //_logger.Error("HomeController - GetReports", exception);
                return View("Error");
            }
            //return View("Error");            
        }
  
        public void Post(HttpPostedFileWrapper file)
        {
            string path = @"D:\Temp\";

            if (file != null)
                file.SaveAs(path + file.FileName);

            //return RedirectToAction("Index");
        }

    }
}
